package com.cugmarket.controller.chat;


import com.cugmarket.constant.MessageConstant;
import com.cugmarket.dto.Result;
import com.cugmarket.entity.ChatList;
import com.cugmarket.service.ChatListService;
import com.cugmarket.utils.UserHolder;
import com.cugmarket.vo.ChatListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/chat/list")
public class ChatListController {
    @Autowired
    private ChatListService chatListService;

    /**
     * 创建聊天列表
     * @param chatList
     * @return
     */
    @PostMapping
    public Result<ChatList> create(@RequestBody ChatList chatList){
        ChatList chat = chatListService.create(chatList);
        return Result.success(chat);
    }


    /**
     * 查询当前用户的所有聊天列表
     * @return
     */
    @GetMapping("/all")
    public Result<List<ChatListVo>> getMyChatList(){
        List<ChatListVo> list =  chatListService.getMyChatList();
        return Result.success(list);
    }

    /**
     * 查询当前用户在所有聊天列表中未读消息的总数
     * @return
     */
    @GetMapping("/noRead")
    public Result getNoReadTotal(){
        int total = chatListService.getNoReadTotal();
        return Result.success(total);
    }

    /**
     * 根据用户id和商品id获取聊天列表id
     * @param toUserId
     * @param productId
     * @return
     */
    @GetMapping("/getId")
    public Result getChatListId( @RequestParam String toUserId,@RequestParam String productId) {
        String fromUserId = UserHolder.getUser();
        ChatList chatListId = chatListService.getChatListId(fromUserId, toUserId, productId);
        if (chatListId == null) {
            // 没有旧的聊天列表
            return Result.error(MessageConstant.SEARCH_ERROR);
        }
        return Result.success(chatListId);
    }


    /**
     * 根据聊天列表 ID 删除聊天列表
     * @param chatListId 聊天列表的 ID
     * @return 操作结果
     */
    @DeleteMapping("/{chatListId}")
    public Result deleteChatList(@PathVariable String chatListId) {
        chatListService.deleteChatList(chatListId);
        return Result.success();
    }

}
